﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Maticsoft.BLL
{
    /// <summary>
    /// 购物车
    /// </summary>
    public class ShoppingCar
    {
        /// <summary>
        /// 购物车
        /// </summary>
        private Model.ShoppingCar _ShoppingCar = null;
        public ShoppingCar()
        {
            _ShoppingCar = new Model.ShoppingCar();
        }
        /// <summary>
        /// 更新购物车明细
        /// </summary>
        /// <param name="oitModel">明细</param>
        /// <returns>返回状态</returns>
        public int UpdateShoppingCar(Model.Oitems oitModel)
        {
            BLL.Products proBLL = new Products();
            Model.Products proModel = proBLL.GetModel((int)oitModel.ProductId);
            if (proModel == null)
            {
                return -1;
            }

            if (proModel.StoreNum < oitModel.ProductNum)
            {
                return -2;
            }

            if (proModel.MaxNum > 0 && proModel.MaxNum < oitModel.ProductNum)
            {
                return -3;
            }

            _ShoppingCar = GetShoppingCar();

            List<Model.Oitems> oitemList = _ShoppingCar.OitemList;
            if (oitemList.Count > 0)
            {
                List<Model.Oitems> nList = oitemList.FindAll(n => n.ProductId == oitModel.ProductId);
                if (nList.Count > 0)
                {
                    int currNum  =(int)(nList[0].ProductNum + oitModel.ProductNum);
                    if (proModel.StoreNum < currNum)
                    {
                        return -2;
                    }

                    if (proModel.MaxNum > 0 && proModel.MaxNum < currNum)
                    {
                        return -3;
                    }

                    if (proModel.MinNum > 0 && proModel.MinNum > currNum)
                    {
                        return -4;
                    }
                    oitModel.ProductNum = currNum;
                    oitModel.TotalPrice = oitModel.SalePrice * currNum;
                    oitemList.Remove(nList[0]);
                }

                if (oitModel.ProductNum > 0)
                {
                    oitemList.Add(oitModel);
                }
            }
            else
            {
                oitemList.Add(oitModel);
            }

            _ShoppingCar = CalculateShoppingCar(_ShoppingCar);

            HttpContext.Current.Session["ShoppingCar"] = _ShoppingCar;

            return 1;
        }
        /// <summary>
        /// 更新购物车主体
        /// </summary>
        /// <param name="ordModel">主体</param>
        /// <returns>返回状态</returns>
        public int UpdateShoppingCar(Model.Orders ordModel)
        {
            _ShoppingCar = GetShoppingCar();

            _ShoppingCar.Order = ordModel;

            _ShoppingCar = CalculateShoppingCar(_ShoppingCar);

            HttpContext.Current.Session["ShoppingCar"] = _ShoppingCar;

            return 1;
        }
        /// <summary>
        /// 计算购物车主体价格数量
        /// </summary>
        /// <param name="shoppingCar">购物车</param>
        /// <returns>返回购物车</returns>
        private Model.ShoppingCar CalculateShoppingCar(Model.ShoppingCar shoppingCar)
        {
            Model.Orders orderModel = shoppingCar.Order;
            orderModel.ProductPrice = 0;
            orderModel.OitemNum = 0;
            orderModel.CouponFee = 0;
            orderModel.DeliveryFee = 0;
            orderModel.InsureFee = 0;
            orderModel.TaxFee = 0;
            List<Model.Oitems> oitemList = shoppingCar.OitemList;
            foreach (Model.Oitems o in oitemList)
            {
                orderModel.ProductPrice += (decimal)o.TotalPrice;
                orderModel.OitemNum += (int)o.ProductNum;
                orderModel.DeliveryFee += (decimal)o.DeliveryFee;
                orderModel.InsureFee += (decimal)o.InsureFee;
                orderModel.TaxFee += (decimal)o.TaxFee;
            }

            orderModel.Total = orderModel.ProductPrice + orderModel.DeliveryFee + orderModel.InsureFee + orderModel.TaxFee - orderModel.CouponFee;

            shoppingCar.Order = orderModel;

            return shoppingCar;
        }
        /// <summary>
        /// 获取购物车信息
        /// </summary>
        /// <returns>购物车实体</returns>
        public Model.ShoppingCar GetShoppingCar()
        {
            if (HttpContext.Current.Session["ShoppingCar"] != null)
            {
                _ShoppingCar = HttpContext.Current.Session["ShoppingCar"] as Model.ShoppingCar;            
            }

            return _ShoppingCar;
        }
        /// <summary>
        /// 清空购物车
        /// </summary>
        public void ClearShoppingCar()
        {
            HttpContext.Current.Session["ShoppingCar"] = null;
        }
    }
}
